<template>
	<div class="edit-popup">
		<popup ref="popupRef" :title="popupTitle" :async="true" width="880px"  cancelButtonText="" confirmButtonText=""
			@close="handleClose">
			<div class="mt-[-28px]">
				<el-card class="!border-none " v-loading="pager.loading" shadow="never">
					<div class="mb-[15px]">
						<el-alert type="warning" title="卡单：指用户第几个订单，订单金额跟佣金后台自定义。 优先级：手动派单 > 卡单 > 用户组 > 自动派单" :closable="false" show-icon />
					</div>
					<el-button v-perms="['user.user_kadan/add']" type="primary" @click="handleAdd">
						<template #icon>
							<icon name="el-icon-Plus" />
						</template>
						新增
					</el-button>
					<div class="mt-4">
						<el-table :data="pager.lists">
							<el-table-column label="第几单" prop="num" show-overflow-tooltip />
							<el-table-column label="订单金额" prop="money" show-overflow-tooltip />
							<el-table-column label="佣金" prop="commission" show-overflow-tooltip />
							<el-table-column label="状态" prop="status" show-tooltip-when-overflow>
								<template #default="{ row }">
									<div v-if="row.status==0">进行中</div>
									<div v-if="row.status==1">已完成</div>
								</template>
							</el-table-column>
							<el-table-column label="时间" min-width="180" show-tooltip-when-overflow>
								<template #default="{ row }">
									<div>创建时间：{{row.create_time}}</div>
									<div>完成时间：{{row.finish_time}}</div>
								</template>
							</el-table-column>
							<el-table-column label="操作" width="120" fixed="right">
								<template #default="{ row }">
									<el-button  v-if="row.status==0" v-perms="['user.user_kadan/edit']" type="primary" link @click="handleEdit(row)">
										编辑
									</el-button>
									<el-button v-perms="['user.user_kadan/delete']" type="danger" link
										@click="handleDelete(row.id)">
										删除
									</el-button>
								</template>
							</el-table-column>
						</el-table>
					</div>
					<div class="flex mt-4 justify-end">
						<pagination v-model="pager" @change="getLists" />
					</div>
				</el-card>
				<edit-popup v-if="showEdit" ref="editRef"  @success="getLists" @close="showEdit = false" />
			</div>
		</popup>
	</div>
</template>

<script lang="ts" setup name="goodsEdit">
	import Popup from '@/components/popup/index.vue'
	import { usePaging } from '@/hooks/usePaging'
	import { apiUserKadanLists, apiUserKadanDelete } from '@/api/user_kadan'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'

	const emit = defineEmits(['success', 'close'])
	const popupRef = shallowRef<InstanceType<typeof Popup>>()

	// 弹窗标题
	const popupTitle = computed(() => {
		return '卡单规则'
	})
	
	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	// 是否显示编辑框
	const showEdit = ref(false)
	
	const state = reactive({
		user_id: 0
	})
	
	
	// 查询条件
	const queryParams = reactive({
		user_id: state.user_id,
	})
	
	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiUserKadanLists,
		params: queryParams
		
	})
	
	// 添加
	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
		editRef.value?.setFormData({user_id:state.user_id})
	}
	
	// 编辑
	const handleEdit = async (data : any) => {
		data['user_id'] = state.user_id
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}
	
	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiUserKadanDelete({ id })
		getLists()
	}

	//打开弹窗
	const open = () => {
		popupRef.value?.open()
	}

	// 关闭回调
	const handleClose = () => {
		emit('close')
	}
	
	const setFormData = async (data : Record<any, any>) => {
		state.user_id = data.user_id
		queryParams.user_id = data.user_id
		getLists()
	}

	defineExpose({
		open,
		setFormData
	})
	
</script>